package com.briup.estore.service.impl;

import com.briup.estore.common.constant.ExceptionMsgEnum;
import com.briup.estore.common.dto.UserInfoUpdateDTO;
import com.briup.estore.common.dto.UserLoginDto;
import com.briup.estore.common.dto.UserPasswordUpdateDTO;
import com.briup.estore.common.dto.UserRegisterDTO;
import com.briup.estore.common.exception.EstoreException;
import com.briup.estore.common.pojo.User;
import com.briup.estore.common.utils.BeanCopyUtils;
import com.briup.estore.common.utils.JWTUtils;
import com.briup.estore.common.vo.UserInfoVO;
import com.briup.estore.dao.BookDao;
import com.briup.estore.dao.UserDao;
import com.briup.estore.dao.impl.UserDaoImpl;
import com.briup.estore.service.UserService;

import java.sql.Timestamp;
import java.util.Objects;

/**
 * @Author briup-adam
 * @Date 2023/8/3 下午2:16
 * @Description
 **/

public class UserServiceImpl implements UserService {

    private UserDao userDao=new UserDaoImpl();

    @Override
    public String login(UserLoginDto dto) {
        if (Objects.isNull(dto.getUsername()))
            throw  new EstoreException(ExceptionMsgEnum.USER_USERNAME_NOT_NULL);

        if (Objects.isNull(dto.getPassword()))
            throw  new EstoreException(ExceptionMsgEnum.USER_PASSWORD_NOT_NULL);

        User user = userDao.selectByUsername(dto.getUsername());
        if (Objects.isNull(user)){
            throw  new EstoreException(ExceptionMsgEnum.USER_NOT_EXITS);
        }else {
            if (user.getPassword().equals(dto.getPassword())){
                String jwt = JWTUtils.createJWT(String.valueOf(user.getId()));
                return  jwt;
            }else {
                throw  new EstoreException(ExceptionMsgEnum.USER_PASSWORD_IS_ERROR);
            }
        }
    }

    @Override
    public void register(UserRegisterDTO dto) {
        User user = BeanCopyUtils.copyBean(dto, User.class);
        user.setRegisterTime(new Timestamp(System.currentTimeMillis()));
        User u = userDao.selectByUsername(user.getUsername());
        if (u==null){
            userDao.save(user);
        }else {
            throw  new  EstoreException(ExceptionMsgEnum.USER_USERNAME_IS_EXIST);
        }


    }

    @Override
    public UserInfoVO getUserInfo(Integer userId) {
        User user = userDao.getUserById(userId);
        UserInfoVO userInfoVo = BeanCopyUtils.copyBean(user, UserInfoVO.class);
        return userInfoVo;
    }

    @Override
    public UserInfoVO updateUserMessage(UserInfoUpdateDTO userInfoUpdateDTO) {
        User user = BeanCopyUtils.copyBean(userInfoUpdateDTO, User.class);
        User select = userDao.selectByUsername(user.getUsername());
        if (select==null||select.getId()==user.getId()){
            userDao.updateUser(user);
        }else {
            throw  new EstoreException(ExceptionMsgEnum.USER_USERNAME_IS_EXIST);
        }


       return getUserInfo(user.getId());

    }

    @Override
    public void updatePassword(int userId, UserPasswordUpdateDTO userPasswordUpdateDTO) {

        String newPassword = userPasswordUpdateDTO.getNewPassword();
        String oldPassword = userPasswordUpdateDTO.getOldPassword();
        if (newPassword.equals(oldPassword)){
            throw  new  EstoreException(ExceptionMsgEnum.SAME_OLD_PASSWORD);
        }
        User user = userDao.getUserById(userId);
        if (!user.getPassword().equals(oldPassword)){
            throw  new   EstoreException(ExceptionMsgEnum.PASSWORD_ERROR);
        }
        userDao.updateUserPassword(newPassword,userId);
    }
}
